package cn.itcast.controller;

import cn.itcast.constant.MessageConstant;
import cn.itcast.interfaces.OrderSettingService;
import cn.itcast.pojo.OrderSetting;
import cn.itcast.pojo.Result;
import cn.itcast.utils.POIUtils;
import com.alibaba.dubbo.config.annotation.Reference;
import io.reactivex.Observable;
import org.apache.log4j.Logger;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author: peterChen
 * @date: 2019/8/14 8:53
 * @description: TODO
 * @version: 1.0
 */
@RestController
@RequestMapping("ordersetting")
public class OrderSettingController {

    @Reference
    private OrderSettingService orderSettingService;

    private Logger log = Logger.getLogger(OrderSettingController.class);


    /**
     * 上传文件模块（excel表格，循环预约设置）
     * @param excelFile
     * @return
     */
    @RequestMapping("/upload")
    public Result upload(@RequestParam("excelFile")MultipartFile excelFile){
        try {
            //读取excel文件，获取文件中内容集合
            List<String[]> list = POIUtils.readExcel(excelFile);
            List<OrderSetting> orderSettings = new ArrayList<>();
            //遍历，将数据封装到ordersetting中
            /*for (String[] strings : list) {   //strings:[date,number]
                OrderSetting orderSetting = new OrderSetting(new Date(strings[0]),Integer.parseInt(strings[1]));
                orderSettings.add(orderSetting);
            }*/
            //使用Rxjava遍历list集合
            Observable.fromIterable(list).subscribe((strings)->{
                OrderSetting orderSetting = new OrderSetting(new Date(strings[0]),Integer.parseInt(strings[1]));
                orderSettings.add(orderSetting);
            });
            //调用service层方法导入文件内容
            orderSettingService.add(orderSettings);
            return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
        } catch (IOException e) {
            //e.printStackTrace();
            log.error("upload file error.",e);
            return new Result(false,MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
    }


    /**
     * 根据月份获取对应月份的预约设置数据
     * @param Date
     * @return
     */
    @RequestMapping("/getOrderSettingByMonth")
    public Result getOrderSettingByMonth(String Date){

        try {
            //获取本月份的（第一天到最后一天）的预约设置数据
            List<Map> list = orderSettingService.getOrderSettingByMonth(Date);
            return new Result(true,MessageConstant.GET_ORDERSETTING_SUCCESS,list);
        } catch (Exception e) {
            //e.printStackTrace();
            log.error("get ordersetting error.",e);
            return new Result(false,MessageConstant.GET_ORDERSETTING_FAIL);
        }
    }

    /**
     * 页面编辑后提交的可预约人数设置
     * @param orderSetting
     * @return
     */
    @RequestMapping("/editNumberByDate")
    public Result editNumberByDate(@RequestBody OrderSetting orderSetting){
        try {
            //页面设置可预约人数
            orderSettingService.editNumberByDate(orderSetting);
            return new Result(true,MessageConstant.ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            //e.printStackTrace();
            log.error("edit ordersetting error.",e);
            return new Result(false,MessageConstant.ORDERSETTING_FAIL);
        }
    }
}
